﻿using System;
using UnityEngine;

public class ComboBox : MonoBehaviour
{
    private static bool forceToUnShow;
    private static bool isClickedComboButton;
    private static int selectedItemIndex;
    private static int useControlID = -1;

    public static int GetSelectedItemIndex()
    {
        return selectedItemIndex;
    }

    public static int List(Rect rect, string buttonText, GUIContent[] listContent, GUIStyle listStyle)
    {
        return List(rect, new GUIContent(buttonText), listContent, "button", "box", listStyle);
    }

    public static int List(Rect rect, GUIContent buttonContent, GUIContent[] listContent, GUIStyle listStyle)
    {
        return List(rect, buttonContent, listContent, "button", "box", listStyle);
    }

    public static int List(Rect rect, string buttonText, GUIContent[] listContent, GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle)
    {
        return List(rect, new GUIContent(buttonText), listContent, buttonStyle, boxStyle, listStyle);
    }

    public static int List(Rect rect, GUIContent buttonContent, GUIContent[] listContent, GUIStyle buttonStyle, GUIStyle boxStyle, GUIStyle listStyle)
    {
        if (forceToUnShow)
        {
            forceToUnShow = false;
            isClickedComboButton = false;
        }
        bool flag = false;
        int controlID = GUIUtility.GetControlID(FocusType.Passive);
        if ((Event.current.GetTypeForControl(controlID) == EventType.MouseUp) && isClickedComboButton)
        {
            flag = true;
        }
        if (GUI.Button(rect, buttonContent, buttonStyle))
        {
            if (useControlID == -1)
            {
                useControlID = controlID;
                isClickedComboButton = false;
            }
            if (useControlID != controlID)
            {
                forceToUnShow = true;
                useControlID = controlID;
            }
            isClickedComboButton = true;
        }
        if (isClickedComboButton)
        {
            Rect position = new Rect(rect.x, rect.y + listStyle.CalcHeight(listContent[0], 1f), rect.width, listStyle.CalcHeight(listContent[0], 1f) * listContent.Length);
            GUI.Box(position, string.Empty, boxStyle);
            int num2 = GUI.SelectionGrid(position, selectedItemIndex, listContent, 1, listStyle);
            if (num2 != selectedItemIndex)
            {
                selectedItemIndex = num2;
            }
        }
        if (flag)
        {
            isClickedComboButton = false;
        }
        return GetSelectedItemIndex();
    }
}

